package simulador;

import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_from_p;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_system;

public class Locker extends Sim_entity {

	private Sim_port in, inDiskControl, out;

	public Locker(String name) {
		super(name);
		in = new Sim_port("In");
		inDiskControl = new Sim_port("InDiskControl");
		out = new Sim_port("Out");
		add_port(in);
		add_port(inDiskControl);
		add_port(out);
	}

	public void body() {
		while (Sim_system.running()) {
			Sim_event e = new Sim_event();
			// Get the next event
			// Event from CPU
			sim_get_next(new Sim_from_p(Sim_system.get_entity_id("CPU")), e);
			if (e.get_tag() == -1)
				continue;
			// Get the event's data
			Job job = (Job) e.get_data();
			// The event has completed service
			sim_completed(e);
			// Even I/O jobs go to disk
			// sim_schedule(out, 0.0, 2, job);
			sim_schedule(out, 0.0, e.get_tag(), job);
			// Waiting event from Disk
			sim_trace(4, "Job(" + e.get_tag() + ") sent to Disk");
			sim_get_next(new Sim_from_p(Sim_system.get_entity_id("Disk")), e);
			// The event has completed service e loop go on
			sim_completed(e);
		}
	}

}
